package org.linlinjava.litemall.admin.service;





import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.linlinjava.litemall.db.domain.LitemallCarBrand;
import org.linlinjava.litemall.db.domain.LitemallCarModel;
import org.linlinjava.litemall.db.domain.LitemallCarSeries;
import org.linlinjava.litemall.db.service.LitemallCarBrandService;
import org.linlinjava.litemall.db.service.LitemallCarModelService;
import org.linlinjava.litemall.db.service.LitemallCarSeriesService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;


/**
 *
 */
@Component
@Service
public class AdminCarBrandService {

    @Autowired
    AdminCarSeriesService carSeriesService;

    @Autowired
    LitemallCarBrandService litemallCarBrandService;

    @Autowired
    LitemallCarSeriesService litemallCarSeriesService;

    @Autowired
    LitemallCarModelService litemallCarModelService;

    @Autowired
    AdminCarBrandService carBrandService;

    private final Log logger = LogFactory.getLog(AdminCarBrandService.class);

    public void httpRequest() {

        //汽车之家车辆品牌信息接口
        String apiPath = "https://www.autohome.com.cn/ashx/index/GetHomeFindCar.ashx";
        //创建httpclient实例
        CloseableHttpClient client = HttpClients.createDefault();
        //创建httpget实例
        HttpGet httpGet = new HttpGet(apiPath);
        try{
            //规范httpget的报头信息
            httpGet.addHeader("content-type","text/xml");
            //执行httpget,获取数据信息
            HttpResponse response = client.execute(httpGet);
            if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                //判断从接口是否获取数据之后,再对数据进行解析,得到字符串
                String result = EntityUtils.toString(response.getEntity());
                String detail = result.substring(22);

                //对json数组进行解析,将字符串解析为json数组并遍历读取
                JSONArray jsonArray = JSONArray.parseArray(detail);
                for (int i = 0; i < jsonArray.size(); i++) {
                    //对实体对象实例化
                    LitemallCarBrand CarBrand = new LitemallCarBrand();
                    //将json数组的每一个对象都解析为jsonObject
                    JSONObject object=jsonArray.getJSONObject(i);
                    //将json对象中的数据存入实体化对象中
                    int brandId = object.getInteger("id");
                    CarBrand.setId(brandId);
                    CarBrand.setName(object.getString("name"));
                    CarBrand.setGroup(object.getString("letter"));
                    CarBrand.setDeleted((byte) 0);
                    //判断品牌id是否在数据库中为空,为空说明数据需要插入
                    if (litemallCarBrandService.findById(brandId) == null){
                        litemallCarBrandService.add(CarBrand);
                    }
                    //根据brandid重复调用series的方法,接着更新和插入series表里的数据
                    carSeriesService.httpRequest(CarBrand.getId());
                }
            }

        }catch (Exception e){
            logger.error(e.getMessage(),e);
            throw new RuntimeException(e);
        }

    }

    public List<LitemallCarBrand> list(){
        List<LitemallCarBrand> carBrands = litemallCarBrandService.selectbrandList();

        for (int i = 0; i < carBrands.size(); i++) {
            List<LitemallCarSeries> carSeries = litemallCarSeriesService.selectSeriesList(String.valueOf(carBrands.get(i).getId()));
            List<LitemallCarSeries> finalcarSeries = new ArrayList<>();
            for (int j = 0; j < carSeries.size(); j++) {
                LitemallCarSeries series = new LitemallCarSeries();
                BeanUtils.copyProperties(carSeries.get(j),series);
//                List<LitemallCarModel> carModels = litemallCarModelService.selectModelList(String.valueOf(series.getId()));
//                List<LitemallCarModel> finalcarModels = new ArrayList<>();
//                for (int k = 0; k < carModels.size(); k++) {
//                    LitemallCarModel model = new LitemallCarModel();
//                    BeanUtils.copyProperties(carModels.get(k),model);
//                    finalcarModels.add(model);
//                }
//                series.setChildren(finalcarModels);
                finalcarSeries.add(series);
            }
            carBrands.get(i).setChildren(finalcarSeries);
        }
        System.out.println(carBrands);

//        List<LitemallCarSeries> carSeries = litemallCarSeriesService.selectSeriesList();
//        List<LitemallCarModel> carModels = litemallCarModelService.selectModelList();
        return  carBrands;
    }
}
